﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Power.DBUtility;//Please add references
namespace Power.DAL
{
	/// <summary>
	/// 数据访问类:Com_infoDAO
	/// </summary>
	public partial class Com_infoDAO
	{
		public Com_infoDAO()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string com_port,string station_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from com_info");
			strSql.Append(" where com_port=@com_port and station_id=@station_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@com_port", SqlDbType.VarChar,50),
					new SqlParameter("@station_id", SqlDbType.VarChar,20)			};
			parameters[0].Value = com_port;
			parameters[1].Value = station_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Power.Model.Com_info model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into com_info(");
			strSql.Append("com_port,baud_rate,data_bit,odd,stop_bit,station_id,scan_cycle,node_count,read_head)");
			strSql.Append(" values (");
			strSql.Append("@com_port,@baud_rate,@data_bit,@odd,@stop_bit,@station_id,@scan_cycle,@node_count,@read_head)");
			SqlParameter[] parameters = {
					new SqlParameter("@com_port", SqlDbType.VarChar,50),
					new SqlParameter("@baud_rate", SqlDbType.VarChar,50),
					new SqlParameter("@data_bit", SqlDbType.VarChar,50),
					new SqlParameter("@odd", SqlDbType.VarChar,20),
					new SqlParameter("@stop_bit", SqlDbType.VarChar,10),
					new SqlParameter("@station_id", SqlDbType.VarChar,20),
					new SqlParameter("@scan_cycle", SqlDbType.Int,4),
					new SqlParameter("@node_count", SqlDbType.Int,4),
					new SqlParameter("@read_head", SqlDbType.VarChar,50)};
			parameters[0].Value = model.com_port;
			parameters[1].Value = model.baud_rate;
			parameters[2].Value = model.data_bit;
			parameters[3].Value = model.odd;
			parameters[4].Value = model.stop_bit;
			parameters[5].Value = model.station_id;
			parameters[6].Value = model.scan_cycle;
			parameters[7].Value = model.node_count;
			parameters[8].Value = model.read_head;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Power.Model.Com_info model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update com_info set ");
			strSql.Append("baud_rate=@baud_rate,");
			strSql.Append("data_bit=@data_bit,");
			strSql.Append("odd=@odd,");
			strSql.Append("stop_bit=@stop_bit,");
			strSql.Append("scan_cycle=@scan_cycle,");
			strSql.Append("node_count=@node_count,");
			strSql.Append("read_head=@read_head");
			strSql.Append(" where com_port=@com_port and station_id=@station_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@baud_rate", SqlDbType.VarChar,50),
					new SqlParameter("@data_bit", SqlDbType.VarChar,50),
					new SqlParameter("@odd", SqlDbType.VarChar,20),
					new SqlParameter("@stop_bit", SqlDbType.VarChar,10),
					new SqlParameter("@scan_cycle", SqlDbType.Int,4),
					new SqlParameter("@node_count", SqlDbType.Int,4),
					new SqlParameter("@read_head", SqlDbType.VarChar,50),
					new SqlParameter("@com_port", SqlDbType.VarChar,50),
					new SqlParameter("@station_id", SqlDbType.VarChar,20)};
			parameters[0].Value = model.baud_rate;
			parameters[1].Value = model.data_bit;
			parameters[2].Value = model.odd;
			parameters[3].Value = model.stop_bit;
			parameters[4].Value = model.scan_cycle;
			parameters[5].Value = model.node_count;
			parameters[6].Value = model.read_head;
			parameters[7].Value = model.com_port;
			parameters[8].Value = model.station_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string com_port,string station_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from com_info ");
			strSql.Append(" where com_port=@com_port and station_id=@station_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@com_port", SqlDbType.VarChar,50),
					new SqlParameter("@station_id", SqlDbType.VarChar,20)			};
			parameters[0].Value = com_port;
			parameters[1].Value = station_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Power.Model.Com_info GetModel(string com_port,string station_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 com_port,baud_rate,data_bit,odd,stop_bit,station_id,scan_cycle,node_count,read_head from com_info ");
			strSql.Append(" where com_port=@com_port and station_id=@station_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@com_port", SqlDbType.VarChar,50),
					new SqlParameter("@station_id", SqlDbType.VarChar,20)			};
			parameters[0].Value = com_port;
			parameters[1].Value = station_id;

			Power.Model.Com_info model=new Power.Model.Com_info();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Power.Model.Com_info DataRowToModel(DataRow row)
		{
			Power.Model.Com_info model=new Power.Model.Com_info();
			if (row != null)
			{
				if(row["com_port"]!=null)
				{
					model.com_port=row["com_port"].ToString();
				}
				if(row["baud_rate"]!=null)
				{
					model.baud_rate=row["baud_rate"].ToString();
				}
				if(row["data_bit"]!=null)
				{
					model.data_bit=row["data_bit"].ToString();
				}
				if(row["odd"]!=null)
				{
					model.odd=row["odd"].ToString();
				}
				if(row["stop_bit"]!=null)
				{
					model.stop_bit=row["stop_bit"].ToString();
				}
				if(row["station_id"]!=null)
				{
					model.station_id=row["station_id"].ToString();
				}
				if(row["scan_cycle"]!=null && row["scan_cycle"].ToString()!="")
				{
					model.scan_cycle=int.Parse(row["scan_cycle"].ToString());
				}
				if(row["node_count"]!=null && row["node_count"].ToString()!="")
				{
					model.node_count=int.Parse(row["node_count"].ToString());
				}
				if(row["read_head"]!=null)
				{
					model.read_head=row["read_head"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select com_port,baud_rate,data_bit,odd,stop_bit,station_id,scan_cycle,node_count,read_head ");
			strSql.Append(" FROM com_info ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" com_port,baud_rate,data_bit,odd,stop_bit,station_id,scan_cycle,node_count,read_head ");
			strSql.Append(" FROM com_info ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM com_info ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.station_id desc");
			}
			strSql.Append(")AS Row, T.*  from com_info T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "com_info";
			parameters[1].Value = "station_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

